package com.jevons.muffin.net.observer;

import com.jevons.muffin.net.bean.DownloadResponse;
import com.jevons.muffin.net.exception.ResponseException;
import com.jevons.muffin.net.inter.DownloadObserver;

import io.reactivex.annotations.NonNull;

/**
 * 下载任务及其回调
 * @author linaisheng
 * Created at 2019/3/25.
 */

public class DownloadDisposable extends BaseDisposable<DownloadResponse> {

    private static final String TAG = DownloadDisposable.class.getSimpleName();

    private DownloadObserver downloadObserver;

    public DownloadDisposable(String taskId, DownloadObserver dataEngineObserver) {
        super(taskId);
        downloadObserver = dataEngineObserver;
    }

    @Override
    void onResponse(@NonNull DownloadResponse response) {
        if (downloadObserver == null) {
            return;
        }
        if (response.getType() == DownloadResponse.Type.SUCCESS) {
            downloadObserver.responseSuccess(taskId, response.getContent());
            onComplete();
        } else if (response.getType() == DownloadResponse.Type.PROGRESS) {
            downloadObserver.onDownloading(taskId, Integer.valueOf(response.getContent()));
        } else {
            downloadObserver.responseError(taskId,
                    new ResponseException(response.getContent(), null));
            onComplete();
        }
    }
}
